package es.ats.codington.festival.services;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.daos.EventSignUpDAOImpl;
import es.ats.codington.festival.daos.VisitorDAOImpl;
import es.ats.codington.festival.exceptions.DAOSExceptions;
import es.ats.codington.festival.interfaces.services.iLoginVisitor;
import es.ats.codington.festival.interfaces.services.iSignUpEvent;
import es.ats.codington.festival.interfaces.services.iUpdatePass;
import es.ats.codington.festival.interfaces.services.iUpdateVisitor;
import es.ats.codington.festival.interfaces.services.iVisitorRegister;
import es.ats.codington.festival.utils.FERSDataConnection.FersDC;

/**
 * 
 * @author CODINGTONPOJO
 *
 */
public class VisitorService implements iLoginVisitor, iSignUpEvent, iUpdatePass, iUpdateVisitor, iVisitorRegister {

	private Connection connection;
	
	/** Constructor
	 * 
	 * @param connection
	 */

	public VisitorService(Connection connection) {
		super();
		this.connection = connection;
	}

	
	public VisitorService() {
		super();
	}


	@Override
	
	public boolean registerVisitor(Visitor visitante1) throws Exception {
		boolean reg=false;
		
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("Database connection is not available right now. Please try in a few minutes.");
			
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);
			reg=visitordaoimpl.insertVisitor(visitante1);
						
			connection.close();
			
		return reg;
	}

	
	@Override
	public int loginVisitor(Visitor visitor2) throws Exception {
		int idvisitor=0;
		
		
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("Database connection is not available right now. Please try in a few minutes.");
			
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);
			idvisitor=visitordaoimpl.loginVisitor(visitor2);
			
			
			connection.close();
			
		return idvisitor;
	}
		
	
	@Override
	public boolean updateVisitor(Visitor visitante1, int i) throws Exception {
		boolean update=false;
		
		
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("Database connection is not available right now. Please try in a few minutes.");
			
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);
			update=visitordaoimpl.updateVisitor(visitante1, i);
			
			connection.close();
			
		return update;
	}

	@Override
	public boolean updatePassword(Visitor visitante4, String newpassword, int i) throws Exception {
		
		boolean updpas=false;
		
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("Database connection is not available right now. Please try in a few minutes.");
			
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);
			updpas=visitordaoimpl.updatePass(visitante4, newpassword ,i);
			
			
			connection.close();
				
		return updpas;
	}

	@Override
	public boolean signUpEvent(int idvisitor, int idevent) throws Exception {
		
		boolean signup=false;
		
			connection = FersDC.getConnection();
			if (connection == null) throw new IOException("Database connection is not available right now. Please try in a few minutes.");
		
			EventSignUpDAOImpl eventsignupdaoimpl=new EventSignUpDAOImpl(connection);
			signup=eventsignupdaoimpl.signUpToEvent(idvisitor , idevent);
		
			
			connection.close();
		
		return signup;

	}

	@Override
	public boolean unsignUpEvent(int idvisitor, int idevent) throws Exception {
		
		boolean unsignup=false;
		
		connection = FersDC.getConnection();
		if (connection == null) throw new IOException("Database connection is not available right now. Please try in a few minutes.");
		
		EventSignUpDAOImpl eventsignupdaoimpl=new EventSignUpDAOImpl(connection);
		unsignup=eventsignupdaoimpl.unsignUpToEvent(idvisitor , idevent);
		
		
		connection.close();
		
		return unsignup;
	}

}
